#include "stdafx.h"
#include "unicode.h"

// Check a file whether encoded in utf8
bool Utf8FileCheck(fstream &fIn) {
	char byte[3];

	fIn >> byte[0] >> byte[1] >> byte[2];
	
	// Check the three first bytes
	if ((byte[0] & 0xEF) && (byte[1] & 0xBB) && (byte[2] & 0xBF))
		return true;

	return false;
}

// Count the number of a utf8 string
int Utf8Strlen(char *utf8) {
	int n = 0;

	if (utf8) {
		n = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
	}

	return n;
}

// Convert a utf8 string to a wchar_t string
wchar_t* Utf8ToWchar(char *utf8) {
	wchar_t *buffer = NULL;

	if (utf8) {
		int size = Utf8Strlen(utf8);
		
		if (buffer = new wchar_t[size])
			if (!MultiByteToWideChar(CP_UTF8, 0, utf8, -1, buffer, size)) {
				delete []buffer;
				return NULL;
			}
	}

	return buffer;
}